package _go

func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
	return fill(image, sr, sc, newColor, image[sr][sc])
}

//添加一个old参数 也可以直接全局变量
func fill(image [][]int, sr int, sc int, newColor, old int) [][]int {
	if inary(image, sr, sc) == false {
		return image
	}
	if image[sr][sc] == old && image[sr][sc] != newColor {
		image[sr][sc] = newColor
		fill(image, sr-1, sc, newColor, old)
		fill(image, sr, sc-1, newColor, old)
		fill(image, sr, sc+1, newColor, old)
		fill(image, sr+1, sc, newColor, old)
	}
	return image
}

//判断是否有越界
func inary(image [][]int, sr, sc int) bool {
	if sr < 0 || sc < 0 {
		return false
	}
	if sr >= len(image) || sc >= len(image[0]) {
		return false
	}
	return true
}
